package org.droid.campusorder.action;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.droid.campusorder.bean.AdBean;
import org.droid.campusorder.bean.BaseBean;
import org.droid.campusorder.bean.RespAdBean;
import org.droid.campusorder.common.Pre;
import org.droid.campusorder.manager.NetManager;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.os.Handler;

public class RespGetAdAction extends Action {
	Pre p;
	Context c;

	public RespGetAdAction(Context c) {
		super(c);
		this.c = c;

	}

	@Override
	public BaseBean getMsgFromNetwork(Handler handler, String url,
			Hashtable<String, String> hs) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser Parser = factory.newSAXParser();
		XmlHandlerBase dh = new GetAdHandler(c);
		String result = NetManager.getInstance(c).dopostAsString(url, hs);

		XMLReader reader = Parser.getXMLReader();
		// 为XMLReader设置内容处理器
		reader.setContentHandler(dh);
		// 开始解析文件
		reader.parse(new InputSource(new StringReader(result)));

		RespAdBean rab = (RespAdBean) dh.getResult();
		if (rab.getReturnCode() == 1000){
			System.out.println("Ad list size："+rab.getAdBeanList().size());
			return rab;
		}else {
			return null;
		}
		
	}

	class GetAdHandler extends XmlHandlerBase {
		RespAdBean rab;
		AdBean ab;
		String currentTag = null;
		List<AdBean> list;

		public GetAdHandler(Context c) {
			super(c);
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (currentTag != null) {
				String value = new String(ch, start, length);
				if (currentTag.equals("status")) {
					if (value.equals("1000")){
						list = new ArrayList<AdBean>();
					}
					rab.setReturnCode(Integer.parseInt(value));
				} else if (currentTag.equals("name")){
					ab.setName(value);
				} else if (currentTag.equals("type")){
					ab.setType(value);
				} else if (currentTag.equals("desc")){
					ab.setDesc(value);
				} else if (currentTag.equals("pic")){
					ab.setPic(value); 
				}
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			// TODO Auto-generated method stub
			currentTag = null;
			if (localName.equals("ad")){
				list.add(ab);
				ab = null;
			} else if (localName.equals("resp")){
				rab.setAdBeanList(list);
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			currentTag = localName;
			if (localName.equals("resp")) {
				rab = new RespAdBean();
			} else if (localName.equals("ad")) {
				ab = new AdBean();
			}
		}

		@Override
		public BaseBean getResult() {
			// TODO Auto-generated method stub
			return rab;
		}

		@Override
		public boolean storeDataBean(BaseBean dataObject) {
			// TODO Auto-generated method stub
			return false;
		}

	}

	@Override
	public BaseBean getMsgFromDB(String url, Hashtable<String, String> hs)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
